package com.zyb.beiojbackenduserservice.controller.inner;

/**
 * @author 十八岁讨厌编程
 * @date 2023/12/30 17:08
 * @PROJECT_NAME beioj-backend-microservice
 * @description
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zyb.beiojbackendserviceclient.service.UserFeignClient;
import com.zyb.beiojbackenduserservice.service.UserService;
import com.zyb.beiojcommon.common.ErrorCode;
import com.zyb.beiojcommon.exception.BusinessException;
import com.zyb.beiojmodel.model.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 该服务仅内部调用，不是给前端的
 */
@RestController
@RequestMapping("/inner")
public class UserInnerController implements UserFeignClient {

    @Resource
    private UserService userService;

    /**
     * 根据 id 获取用户
     * @param userId
     * @return
     */
    @Override
    @GetMapping("/get/id")
    public User getById(@RequestParam("userId") long userId) {
        return userService.getById(userId);
    }

    /**
     * 根据 id 获取用户列表
     * @param idList
     * @return
     */
    @Override
    @GetMapping("/get/ids")
    public List<User> listByIds(@RequestParam("idList") Collection<Long> idList) {
        return userService.listByIds(idList);
    }

    @Override
    @GetMapping("/get/ak")
    public User getByAk(@RequestParam("accessKey") String accessKey) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getAccessKey,accessKey);
        User one = userService.getOne(userLambdaQueryWrapper);
        if (one == null) throw new BusinessException(ErrorCode.FORBIDDEN_ERROR,"无权限访问");
        return one;
    }

}

